package com.joe.demo.chat.server.handler;

import com.joe.demo.chat.message.GroupJoinRequestMessage;
import com.joe.demo.chat.message.GroupJoinResponseMessage;
import com.joe.demo.chat.server.factory.GroupSessionFactory;
import com.joe.demo.chat.server.session.Group;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @Description
 * @Author gaojw
 * @Date 2024-09-03
 */
@ChannelHandler.Sharable
public class GroupJoinRequestMessageHandler extends SimpleChannelInboundHandler<GroupJoinRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, GroupJoinRequestMessage groupJoinRequestMessage) throws Exception {
        final Group group = GroupSessionFactory.getGroupSession()
                .joinMember(groupJoinRequestMessage.getGroupName(), groupJoinRequestMessage.getUsername());
        if (group != null) {
            channelHandlerContext.writeAndFlush(new GroupJoinResponseMessage(true, groupJoinRequestMessage.getGroupName() + "群加入成功"));
        } else {
            channelHandlerContext.writeAndFlush(new GroupJoinResponseMessage(true, groupJoinRequestMessage.getGroupName() + "群不存在"));
        }
    }
}
